import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Table, Pagination } from 'antd';
import { getShelves, shelvesInit } from '../../../action/actionShelves';
import { cdn } from '../../../config/httpConfig';
import { getShelfState, getTableWidth } from '../../../util/index';
import PaginationConfig from '../../../config/pagination';
import goodDefaultImg from '../../../assets/image/noimg.png';
class ShelfManagement extends Component {
    constructor () {
        super();
        this.query = {
            page: 1,
            page_size: 30,
        };
        const that = this;
        this.pageConfig = Object.assign({}, PaginationConfig, {
            pageSize: that.query.page_size,
            onChange (page, pageSize) {
                that.pageChangeHandle(page, pageSize);
            },
        });
        this.columns = [
            {
                title: '序号',
                dataIndex: 'id',
                width: 60,
                fixed: 'left',
                render: (text, record, index) => <div>{(this.pageConfig.current - 1) * this.pageConfig.pageSize + index + 1}</div>,
            },
            {
                title: '企业名称',
                dataIndex: 'company_title',
                width: 200,
            },
            {
                title: '货点名称',
                dataIndex: 'title',
                width: 150,
            },
            {
                title: '状态',
                dataIndex: 'status',
                width: 150,
                render: (text) => <span>{ getShelfState(text) }</span>,
            },
            {
                title: '操作备注',
                dataIndex: 'status_remark',
                width: 150,
            },
            {
                title: '计划铺设时间',
                dataIndex: 'plan_time',
                width: 250,
                render: (text, record) => <span>{text ? text.substring(0, 13) : ''}</span>,
            },
            {
                title: '铺设时间',
                dataIndex: 'ok_time',
                width: 250,
                render: (text, record) => <span>{text ? text.substring(0, 13) : ''}</span>,
            },
            {
                title: '待撤架时间',
                dataIndex: 'to_cancel_time',
                width: 250,
                render: (text, record) => <span>{text ? text.substring(0, 13) : ''}</span>,
            },
            {
                title: '城市',
                dataIndex: 'city_tile',
                width: 150,
            },
            {
                title: '区域',
                dataIndex: 'area_tile',
                width: 150,
            },
            {
                title: '大厦',
                dataIndex: 'building_title',
                width: 150,
            },
            {
                title: '代理商',
                dataIndex: 'proxy_title',
                width: 150,
            },
            {
                title: '仓库/配送站',
                dataIndex: 'ware_title',
                width: 150,
            },
            {
                title: '补货优先级',
                dataIndex: 'level',
                width: 150,
            },
        ];
    }
    pageChangeHandle (page, pageSize) {
        this.pageConfig.current = this.query.page = page;
        this.query.page_size = pageSize;
        this.getList();
    }
    getList () {
        const { dispatch } = this.props;
        dispatch(getShelves({
            query: this.query,
        }));
    }
    componentWillMount () {
        const { isFetching, shelves } = this.props;
        if (isFetching === 'success' && typeof (shelves) === 'object' && shelves.code === 0) return;
        this.getList();
    }
    componentWillUnmount () {
        const { dispatch } = this.props;
        dispatch(shelvesInit());
    }
    render () {
        const { shelves, isFetching } = this.props;
        let data = [];
        let total = 0;
        if (typeof (shelves) === 'object' && shelves.code === 0) {
            data = shelves.items;
            total = shelves.counts;
            this.pageConfig.current = shelves.page * 1;
        }
        this.pageConfig.total = total;
        return (
            <div className='app-admin-main'>
                <div className='table-wrapper'>
                    <div className='table-wrapper_table'>
                        {<Table
                            pagination={false}
                            loading={isFetching === 'isFetching'}
                            rowKey={record => record.id}
                            scroll={{ x: '140%' }}
                            columns={this.columns} dataSource={data} />}
                        <Pagination {...this.pageConfig} />
                    </div>
                </div>
            </div>
        );
    }
}
function select (state) {
    return {
        shelves: state.shelfList.shelves,
        isFetching: state.requestStatus.status,
    };
}
export default connect(select)(ShelfManagement);
